package com.xxgc.ssm.service.impl;

import com.xxgc.ssm.dao.UsersMapper;
import com.xxgc.ssm.po.Users;
import com.xxgc.ssm.po.UsersExample;
import com.xxgc.ssm.po.info.Info;
import com.xxgc.ssm.service.IUserLogin;
import com.xxgc.ssm.aop.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 用户登录业务逻辑
 * @Author SJY
 * @DATE 2021-12-7
 * 创建类
 * @Author SJY
 * @DATE 2021-12-21
 * 手机号登录功能
 */
@Service
public class UserLoginService extends InfoService implements IUserLogin {
    @Autowired
    private UsersMapper um;

    public Info insertRegister(Users user) {
        //查询手机号是否已经注册
        UsersExample example = new UsersExample();
        example.createCriteria().andUPhoneEqualTo(user.getUPhone());
        List<Users> users = um.selectByExample(example);
        if(users.size()>0){
            info.setCode(-202);
            info.setMsg("手机号已经被注册");
            return info;
        }

        //添加注册时间
        user.setUCreateTime(new Date());
        user.setUAlterTime(new Date());
        int i = um.insertSelective(user);
        if(i==1){
            info.setCode(200);
            info.setMsg("注册成功");
        }else{
            info.setCode(-200);
            info.setMsg("注册失败");
        }
        return info;
    }

    public Info selectLoginByPhone(Users user, HttpSession session) {
        UsersExample example = new UsersExample();
        example.createCriteria()
                .andUPhoneEqualTo(user.getUPhone())//添加电话号码
                .andUPasswordEqualTo(user.getUPassword());//添加密码
        List<Users> users = um.selectByExample(example);//dao层查询
        if(users.size() == 1){
            info.setCode(200);
            info.setMsg("登录成功");
            info.setData(users.get(0));
            session.setAttribute("users",users.get(0));
        }else{
            info.setCode(-203);
            info.setMsg("手机号或密码错误");
        }
        return info;
    }
}
